package array;

public class ZigZadPrintMatrix {
	public static void main(String[] args) {
		int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
		printMatrixInZiaZag(arr);
	}
	
	public static void printMatrixInZiaZag(int[][] matrix) {
		int x1 = 0;
		int y1 = 0;
		int x2 = 0;
		int y2 = 0;
		int endx = matrix.length - 1;
		int endy = matrix[0].length - 1;
		boolean fromUp = false;
		while (x1 != endx + 1) {
			printLevel(matrix,x1,y1,x2,y2,fromUp);
			x1 = y1 ==endy?x1+1:x1;
			y1 = y1== endy?y1:y1+1;
			
			y2 = x2==endx?y2+1:y2;
			x2 = x2==endx?x2:x2+1;
			fromUp =!fromUp;
		}
		System.out.println();

	}

	/** 打印一行数据
	 * 
	 * @param m
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @param f
	 */
	public static void printLevel(int[][] m, int tR, int tC, int dR, int dC, boolean f) {
		if(f){
			while(tR!=dR+1){
				System.out.print(m[tR++][tC--]+" ");
			}
		}else{
			while(dR!=tR-1){
				System.out.print(m[dR--][dC++]+ " ");
			}
		}
	}
}
